<?
// ===================================================
// Students Security
// 8.0.0.0
// ===================================================

// ===========================================================================================================
function recountPrivat() {
	global $privatFull, $privatNews;

	$result = getVarResults("zFoldersCount", "1 GROUP BY uid", "sum(messFull) as messFull, sum(messNew) as messNew, min(uid) as userId");

	while ($zFolders = fetchArray($result)) {
		set_time_limit(0);
		$privatFull[$zFolders[userId]] = $zFolders[messFull];
		$privatNews[$zFolders[userId]] = $zFolders[messNew];
	}

	return true;
}

// ===========================================================================================================
function recountForum() {

	$result = getVarResults("fMessage", "1 GROUP BY uid", "count(mid) as forumFull, min(uid) as userId");

	while ($fMessage = fetchArray($result)) {
		set_time_limit(0);
		$forumFull[$fMessage[userId]] = $fMessage[forumFull];
	}

	return $forumFull;
}

// ===========================================================================================================
function recountBlogs() {

	// Blogs
	// ================================================================================
	$result = getVarResults("blogs", "1 GROUP BY uid", "count(mid) as blogsFull, sum(comment) as commentsFull, min(uid) as userId");

	while ($blogs = fetchArray($result)) {
		set_time_limit(0);
		$blogsFull[$blogs[userId]] = $blogs[blogsFull] * 100000 + $blogs[commentsFull];
	}

	return $blogsFull;
}

// ===========================================================================================================
function recountPhoto() {
	// Photo
	// ================================================================================
	$result = getVarResults("photo", "1 GROUP BY uid", "count(pid) as photoFull, min(uid) as userId");

	while ($photo = fetchArray($result)) {
		set_time_limit(0);
		$photoFull[$photo[userId]] = $photo[photoFull] * 100000;
	}

	return $photoFull;
}

// ===========================================================================================================
function refreshUsersGroup($gender, $forumFull, $photoFull, $blogsFull) {
	global $privatFull, $privatNews;

	$result = getResults("userLogin", "gender='$gender' AND !(status & 1) AND (rank > 0) ORDER BY regTime ASC");
	$place  = 1;

	while ($users = fetchArray($result)) {
		set_time_limit(0);
	    $score = getScore($users, $privatFull[$users['uid']], $forumFull[$users['uid']], $photoFull[$users['uid']], $blogsFull[$users['uid']]);
	
		updateTable("userLogin", "uid=$users[uid]", "currentPlace# privatCount# forumCount# photoCount# blogsCount# rank# messUNew", "$place# ".$privatFull[$users['uid']]."# ".$forumFull[$users['uid']]."# ".$photoFull[$users['uid']]."# ".$blogsFull[$users['uid']]."# $score# ".$privatNews[$users['uid']]);
		$place++;
	}
	
	return true;
}

// ===========================================================================================================
function refreshPlace($uid=0, $gender='M') {

	$users = getOneRow("userLogin", "userLogin.uid=$uid");
	$place = getNumRows("userLogin", "!(status & 1) AND (rank > 0) AND gender='$gender' AND sortTime < $users[sortTime]"); 
	if (!$place) $place = 1;

	updateTable("userLogin", "uid=$uid", "currentPlace", "$place");

	return true;	
}

// ===========================================================================================================
function recountSms() {
	updateTable("smsPhoto", "1", "count", "0");
	$addTime = time() - 10800;

	$result = getVarResults("smsLog", "action=2 AND addTime > $addTime GROUP BY number", "count(sid) AS sid, min(number) AS userId, max(addTime) AS addTime");

	while ($smsLog = fetchArray($result)) {
		updateTable("smsPhoto", "uid=$smsLog[userId]", "count# addTime", "$smsLog[sid]# $smsLog[addTime]");
	}
	
	return true;
}

// ===========================================================================================================
?>